//
//  WLRouterEncryptPopView.m
//  WanLian
//
//  Created by muzhuang on 2018/10/22.
//  Copyright © 2018年 wanlian. All rights reserved.
//

#import "WLRouterEncryptPopView.h"

@interface WLRouterEncryptPopView()<UITableViewDelegate,UITableViewDataSource,UIGestureRecognizerDelegate>

@property (nonatomic, strong) UITableView   *bgView;
@property (nonatomic,copy) void(^action)(NSInteger index);

@end

@implementation WLRouterEncryptPopView

+ (instancetype)showEncryViewAction:(void(^)(NSInteger index))action
{
    WLRouterEncryptPopView *moreView =[[[self class] alloc] initWithAction:action];
    return moreView;
}

- (instancetype)initWithAction:(void(^)(NSInteger index))action
{
    self = [super init];
    if (self) {
        _action = action;
        self.frame = [[UIScreen mainScreen]bounds];//CGRectMake(0, 0, WIDTH_SCREEN_WL, HEIGHT_SCREEN_WL);
        //    背景，带灰度
        self.backgroundColor = wlRGBACOLOR(0, 0, 0, 0.5);
        //    可点击
        self.userInteractionEnabled = YES;
        //    点击背景，收起底部分享面板，移除本视图
        UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tappedCancel)];
        tapGesture.delegate = self;
        [self addGestureRecognizer:tapGesture];
        
        _bgView = [[UITableView alloc] initWithFrame:CGRectMake(0, HEIGHT_SCREEN_WL, WIDTH_SCREEN_WL, WLScaleX(222)+WL_Bottom)];
        _bgView.backgroundColor = wlUIColorRGB(0xFAFAFA);
        _bgView.delegate = self;
        _bgView.dataSource = self;
//        _bgView.rowHeight = WLScaleX(74);
        _bgView.tableFooterView = [UIView new];
        [self addSubview:_bgView];
        
        
        UIWindow *window = [[UIApplication sharedApplication] keyWindow];
        [window addSubview:self];
        //    弹出
        [UIView animateWithDuration:0.25f animations:^{
            self.bgView.frame = CGRectMake(0, HEIGHT_SCREEN_WL - CGRectGetHeight(self.bgView.frame), WIDTH_SCREEN_WL, CGRectGetHeight(self.bgView.frame));
        }];
        
    }
    return self;
}

- (void)tappedCancel
{
    [UIView animateWithDuration:0.25f animations:^{
        [self.bgView setFrame:CGRectMake(0, HEIGHT_SCREEN_WL, WIDTH_SCREEN_WL, 0)];
        self.alpha = 0;
    } completion:^(BOOL finished) {
        if (finished) {
            [self removeFromSuperview];
        }
    }];
}

#pragma mark - UITableView
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 4;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row==0) {
        return WLScaleX(60);
    }
    return WLScaleX(48);
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSInteger row = indexPath.row;
    static NSString *encryptCell = @"encryptCell";
    UITableViewCell *cell =  [tableView dequeueReusableCellWithIdentifier:encryptCell];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:0 reuseIdentifier:encryptCell];
    }
    for (UIView *subView in cell.contentView.subviews) {
        [subView removeFromSuperview];
    }
    
    UILabel *labTitle = [[UILabel alloc] init];
    labTitle.textAlignment = NSTextAlignmentCenter;
    [cell.contentView addSubview:labTitle];
    
    if (row==0) {
        labTitle.frame = CGRectMake(WLScaleX(16), WLScaleX(15), WIDTH_SCREEN_WL-WLScaleX(32), WLScaleX(30));
        labTitle.font = [WLCommonTool fontTypePingFangSCRegular:17];
        labTitle.textColor = wlBlackColor;
        labTitle.text = @"加密类型";
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    else
    {
        labTitle.frame = CGRectMake(WLScaleX(16), WLScaleX(9), WIDTH_SCREEN_WL-WLScaleX(32), WLScaleX(30));
        labTitle.font = [WLCommonTool fontTypePingFangSCRegular:14];
        labTitle.textColor = wlDarkGrayColor;
        cell.selectionStyle = UITableViewCellSelectionStyleBlue;
        if (row==1) {
            labTitle.text = @"WPA2";
        }
        else if (row==2)
        {
            labTitle.text = @"WPA/WPA2";
        }
        else if (row==3)
        {
            labTitle.text = @"无密码";
        }
    }
    
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row >0) {
        if (self.action) {
            self.action(indexPath.row);
        }
        [self tappedCancel];
    }
}

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
    if ([NSStringFromClass([touch.view class]) isEqualToString:@"UITableViewCellContentView"])//||[strTouchView isEqualToString:NSStringFromClass([popView class])]
    {
        return NO;
    }
    else
        return YES;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
